package com.coffee.middleware.dict;


import com.coffee.core.CoffeeResponse;
import com.coffee.core.CoffeeResponseEnum;
import com.coffee.core.ValidGroup;
import com.coffee.middleware.dict.entity.DictDataEntity;
import com.coffee.middleware.dict.repository.DictDataRepository;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author Songkui
 * @since 2024-04-29
 */
@Tag(name = "字典详情管理")
@Slf4j
@RestController
@RequestMapping("/coffee/dict/data")
@AllArgsConstructor
public class DictDataEndpoint {

    private final DictDataRepository dictDataRepository;

    @PostMapping("/insert")
    public CoffeeResponse<Integer> insert(@Validated(ValidGroup.Create.class) @RequestBody DictDataEntity entity){
        Integer id = dictDataRepository.saveDict(entity);
        return CoffeeResponse.responseSuccess(id);
    }

    @PutMapping("/update")
    public CoffeeResponse<?> update(@Validated(ValidGroup.Update.class) @RequestBody DictDataEntity entity){
        dictDataRepository.updateDict(entity);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }

    @Operation(summary = "字典详情列表")
    @GetMapping("/list/{typeId}")
    public CoffeeResponse<List<DictDataEntity>> search(@PathVariable("typeId") Integer typeId){
        return CoffeeResponse.responseSuccess(dictDataRepository.listByType(typeId));
    }

    @GetMapping("/get/{id}")
    public CoffeeResponse<DictDataEntity> get(@PathVariable("id") Integer id){
        return CoffeeResponse.responseSuccess(dictDataRepository.getById(id));
    }

    @DeleteMapping("/delete/{id}")
    public CoffeeResponse<?> delete(@PathVariable("id") Integer id){
        dictDataRepository.removeById(id);
        return CoffeeResponse.response(CoffeeResponseEnum.Success);
    }



}
